<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('新增维修记录（含申请功能）')" />
    <th:block th:include="include :: datetimepicker-css" />
</head>
<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <form class="form-horizontal m" id="form-record-add">
            <div class="col-xs-12">
                <div class="form-group">
                    <label class="col-sm-3 control-label">会议室：</label>
                    <div class="col-sm-8">
                        <select name="roomId" class="form-control" required>
                            <option value="">请选择会议室</option>
                            <option th:each="room : ${rooms}" th:value="${room.roomId}" th:text="${room.roomName + ' - ' + room.location}"></option>
                        </select>
                    </div>
                </div>
            </div>
            <div class="col-xs-12">
                <div class="form-group">
                    <label class="col-sm-3 control-label is-required">申请部长ID：</label>
                    <div class="col-sm-8">
                        <input name="ministerId" class="form-control" type="text" required>
                    </div>
                </div>
            </div>
            <div class="col-xs-12">
                <div class="form-group">
                    <label class="col-sm-3 control-label">问题描述：</label>
                    <div class="col-sm-8">
                        <textarea name="problemDescription" class="form-control"></textarea>
                    </div>
                </div>
            </div>
            <div class="col-xs-12">
                <div class="form-group">
                    <label class="col-sm-3 control-label">维修工：</label>
                    <div class="col-sm-8">
                        <select name="workerId" class="form-control" required>
                            <option value="">请选择维修工</option>
                            <option th:each="worker : ${workers}" th:value="${worker.workerId}" th:text="${worker.workerName + ' - ' + worker.phone}"></option>
                        </select>
                    </div>
                </div>
            </div>
            <div class="col-xs-12">
                <div class="form-group">
                    <label class="col-sm-3 control-label">申请时间：</label>
                    <div class="col-sm-8">
                        <div class="input-group date">
                            <input name="applyTime" class="form-control" placeholder="yyyy-MM-dd" type="text">
                            <span class="input-group-addon"><i class="fa fa-calendar"></i></span>
                        </div>
                    </div>
                </div>
            </div>
            <div class="col-xs-12">
                <div class="form-group">
                    <label class="col-sm-3 control-label">维修状态：</label>
                    <div class="col-sm-8">
                        <select name="status" class="form-control m-b">
                            <option value="已提交" selected>已提交</option>
                        </select>
                    </div>
                </div>
            </div>
           <!-- <div class="col-xs-12">
                <div class="form-group">
                    <label class="col-sm-3 control-label">完成时间：</label>
                    <div class="col-sm-8">
                        <div class="input-group date">
                            <input name="finishTime" class="form-control" placeholder="yyyy-MM-dd" type="text">
                            <span class="input-group-addon"><i class="fa fa-calendar"></i></span>
                        </div>
                    </div>
                </div>
            </div>-->
        </form>
    </div>
    <th:block th:include="include :: footer" />
    <th:block th:include="include :: datetimepicker-js" />
    <script th:inline="javascript">
        var prefix = ctx + "system/record"
        $("#form-record-add").validate({
            focusCleanup: true,
            rules: {
                finishTime: {
                    required: function() {
                        return $("select[name='status']").val() === "已完成";
                    }
                }
            },
            messages: {
                finishTime: {
                    required: "当维修状态为已完成时，必须填写完成时间"
                }
            }
        });

        function submitHandler() {
            if ($.validate.form()) {
                $.operate.save(prefix + "/add", $('#form-record-add').serialize());
            }
        }

        // 设置申请时间默认为今天
        $(document).ready(function() {
            var today = new Date();
            var dd = String(today.getDate()).padStart(2, '0');
            var mm = String(today.getMonth() + 1).padStart(2, '0');
            var yyyy = today.getFullYear();
            today = yyyy + '-' + mm + '-' + dd;
            $("input[name='applyTime']").val(today);
        });

        $("input[name='applyTime']").datetimepicker({
            format: "yyyy-mm-dd",
            minView: "month",
            autoclose: true
        });

        $("input[name='finishTime']").datetimepicker({
            format: "yyyy-mm-dd",
            minView: "month",
            autoclose: true
        });

        // 状态变更时检查完成时间
        $("select[name='status']").change(function() {
            if ($(this).val() === "已完成") {
                $("input[name='finishTime']").attr("required", "required");
            } else {
                $("input[name='finishTime']").removeAttr("required");
            }
        });
    </script>
</body>
</html>